﻿<template>
  <a-modal
    title="编辑会员账户"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="CUSTOMERID " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入CUSTOMERID " v-decorator="['customerid']" />
        </a-form-item>
        <a-form-item label="CUSTOMERNAME " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入CUSTOMERNAME " v-decorator="['customername']" />
        </a-form-item>
        <a-form-item label="ICCODE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入ICCODE " v-decorator="['iccode']" />
        </a-form-item>
        <a-form-item label="ACCOUNTNAME " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入ACCOUNTNAME " v-decorator="['accountname']" />
        </a-form-item>
        <a-form-item label="ACCOUNTCODE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入ACCOUNTCODE " v-decorator="['accountcode']" />
        </a-form-item>
        <a-form-item label="ACCOUNTVALUES " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入ACCOUNTVALUES " v-decorator="['accountvalues']" />
        </a-form-item>
        <a-form-item label="ACCOUNTMONEY " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入ACCOUNTMONEY " v-decorator="['accountmoney']" />
        </a-form-item>
        <a-form-item label="STATE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入STATE " v-decorator="['state']" />
        </a-form-item>
        <a-form-item label="VALUESDATE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择VALUESDATE " v-decorator="['valuesdate']" @change="onChangevaluesdate"/>
        </a-form-item>
        <a-form-item label="CREATEDATE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择CREATEDATE " v-decorator="['createdate']" @change="onChangecreatedate"/>
        </a-form-item>
        <a-form-item label="MODIFYDATE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择MODIFYDATE " v-decorator="['modifydate']" @change="onChangemodifydate"/>
        </a-form-item>
        <a-form-item label="FOUNDER " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入FOUNDER " v-decorator="['founder']" />
        </a-form-item>
        <a-form-item label="FOUNDCODE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入FOUNDCODE " v-decorator="['foundcode']" />
        </a-form-item>
        <a-form-item label="MODIFIER " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入MODIFIER " v-decorator="['modifier']" />
        </a-form-item>
        <a-form-item label="MODIFYCODE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入MODIFYCODE " v-decorator="['modifycode']" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['iD']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import {
    IcAccountEdit
  } from '@/api/modular/main/IcAccountManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        valuesdateDateString: '',
        createdateDateString: '',
        modifydateDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              iD: record.iD,
              customerid: record.customerid,
              customername: record.customername,
              iccode: record.iccode,
              accountname: record.accountname,
              accountcode: record.accountcode,
              accountvalues: record.accountvalues,
              accountmoney: record.accountmoney,
              state: record.state,
              founder: record.founder,
              foundcode: record.foundcode,
              modifier: record.modifier,
              modifycode: record.modifycode
            }
          )
        }, 100)
        this.form.getFieldDecorator('valuesdate', { initialValue: moment(record.valuesdate, 'YYYY-MM-DD') })
        this.valuesdateDateString = moment(record.valuesdate).format('YYYY-MM-DD')
        this.form.getFieldDecorator('createdate', { initialValue: moment(record.createdate, 'YYYY-MM-DD') })
        this.createdateDateString = moment(record.createdate).format('YYYY-MM-DD')
        this.form.getFieldDecorator('modifydate', { initialValue: moment(record.modifydate, 'YYYY-MM-DD') })
        this.modifydateDateString = moment(record.modifydate).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.valuesdate = this.valuesdateDateString
            values.createdate = this.createdateDateString
            values.modifydate = this.modifydateDateString
            IcAccountEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangevaluesdate(date, dateString) {
        this.valuesdateDateString = dateString
      },
      onChangecreatedate(date, dateString) {
        this.createdateDateString = dateString
      },
      onChangemodifydate(date, dateString) {
        this.modifydateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
